﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CompanyManagementDLL.MetaData;

namespace CompanyManagementDLL.DLL
{
    public class RoleDLL
    {
        private readonly CompanyMgtEntities db = new CompanyMgtEntities();
        #region Fecth Data
        /// <summary>
        /// Return role detail based in role id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RoleMetaData GetRoleByID(int id)
        {
            RoleMetaData roleMetaData = new RoleMetaData();
            try
            {
                roleMetaData = db.Roles.Where(r => r.RoleId == id).Select(r => new RoleMetaData
                {
                    RoleId = r.RoleId,
                    RoleName = r.RoleName
                }).FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return roleMetaData;
        }

        #endregion

        #region Get List

        /// <summary>
        /// Retrun Role list
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<RoleMetaData> GetRoleList()
        {
            List<RoleMetaData> roleMetaData = new List<RoleMetaData>();
            try
            {
                roleMetaData = db.Roles.Select(r => new RoleMetaData
                {
                    RoleId = r.RoleId,
                    RoleName = r.RoleName
                }).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return roleMetaData;
        }

        #endregion

        #region DML Opration

        /// <summary>
        /// insert role entity
        /// </summary>
        /// <param name="rolemetadata"></param>
        public void InsertRole(RoleMetaData rolemetadata)
        {
            Role tblRole = new Role();
            try
            {
                tblRole.RoleName = rolemetadata.RoleName;
                db.Roles.Add(tblRole);
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// update role entity
        /// </summary>
        /// <param name="rolemetadata"></param>
        public void UpdateRole(RoleMetaData rolemetadata)
        {
            Role tblRole = new Role();
            try
            {
                tblRole = db.Roles.Where(r => r.RoleId == rolemetadata.RoleId).FirstOrDefault();
                tblRole.RoleName = rolemetadata.RoleName;
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// delete role entity
        /// </summary>
        /// <param name="id"></param>
        public void DeleteRole(int id)
        {
            try
            {
                Role tblRole = new Role();
                tblRole = db.Roles.Where(r => r.RoleId == id).FirstOrDefault();
                db.Roles.Remove(tblRole);
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #endregion

        #region Check Role Assign

        /// <summary>
        /// Check if role is assign to user or not
        /// </summary>
        /// <param name="RoleID"></param>
        /// <returns></returns>
        public bool IsRoleHasUser(int RoleID)
        {
            var hasUser = false;
            try
            {
                //check that role is assign to user or not
                var rolelist = db.Users.Where(u => u.RoleID == RoleID).FirstOrDefault();
                if (rolelist != null)
                {
                    hasUser = true;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

            return hasUser;
        }

        #endregion
    }
}
